﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;

[assembly: WebResource("DynamicExcelImportWebControl.MiniLightbox.js", "text/javascript")]
[assembly: WebResource("DynamicExcelImportWebControl.MiniLightbox.css", "text/css")]
namespace DynamicExcelImportWebControl
{
    public class MiniLightbox : Panel
    {
        private const string JavascriptObjectSuffix = "_minilightbox";
        public string JavascriptObjectId
        {
            get { return this.ClientID + JavascriptObjectSuffix; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //Register the minilightbox script file
            string scriptPath = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "DynamicExcelImportWebControl.MiniLightbox.js");
            this.Page.ClientScript.RegisterClientScriptInclude("MiniLightbox", scriptPath);

            //Register style sheet
            string cssUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "DynamicExcelImportWebControl.MiniLightbox.css");
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = cssUrl;
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            this.Page.Header.Controls.Add(cssLink);

            string overlayScript = "writeLightboxOverlay('minilightboxoverlay');";
            if (!this.Page.ClientScript.IsStartupScriptRegistered("MiniLightboxOverlayStartup"))
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MiniLightboxOverlayStartup", overlayScript, true);
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(this.JavascriptObjectId + " = new MiniLightbox('" + this.ClientID + "', 'minilightboxoverlay');");
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "MiniLightboxStartup", sb.ToString(), true);

            base.Render(writer);
        }
    }
}
